<?php

namespace app\admin\controller;


use app\common\model\QuActive;
use app\common\model\QuAnswer;
use app\common\model\QuClasses;
use app\common\model\QuQuestion;
use app\common\model\QuStudent;
use app\service\UserService;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use think\Db;
use think\Exception;

/**
 * Class Question
 * @package app\admin\controller
 */
class Question extends Base
{

    public function __construct()
    {
        parent::__construct();
    }


    /**
     * 问卷问题
     * @return \think\response\Json
     */
    public function question()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['name']) {
                $where['name'] = [
                    'like', "%{$data['name']}%"
                ];
            }
            if ($data['aid']) {
                $where['aid'] = $data['aid'];
            }
            $list = QuQuestion::getPage($where, $page, $size, 'id desc');
            $total = QuQuestion::getCount($where);
            if (!$list) {
                return returnJson(200, '', []);
            } else {
                $num = range('A', 'Z');
                foreach ($list as $key => $value) {
                    $right = [];
                    $answer = json_decode($value['answer'], true);
                    if ($answer) {
                        foreach ($answer as $k => $val) {
                            $answer[$k]['sign'] = $num[$k];
                            if ($val['is_right']) {
                                $right[] = $num[$k];
                            }
                        }
                    }
                    $list[$key]['answer_arr'] = $answer;
                    $list[$key]['answer'] = json_decode($value['answer'], true);
                    $list[$key]['right_arr'] = implode('、', $right);
                    $list[$key]['active'] = QuActive::getValue(['id' => $value['aid']], 'name');
                }
            }
            return returnJson(200, '', [
                'list' => $list,
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }

    /**
     * 问卷问题编辑/添加
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function question_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuQuestion::getOne(['id' => $data['id']]);
                if (!$active) {
                    return returnJson(202, '问题不存在');
                }
            }
            if (!$data['name']) {
                return returnJson(202, '请填入题干');
            }
            if (!$data['type']) {
                return returnJson(202, '请选择题目类型');
            }
            if ($data['type'] != 4 && $data['type']!=5) {
                if (!$data['answer']) {
                    return returnJson(202, '请填写选项');
                } else {
                    $right = [];
                    foreach ($data['answer'] as $key => $value) {
                        if ($value['is_right']) {
                            $right[] = $value['content'];
                        }
                    }
                    if (!$right) {
                        return returnJson(202, '请勾选至少一个正确答案');
                    }
                }
            }
            $save_data = [
                'name' => $data['name'],
                'score' => $data['score'],
                'aid' => $data['aid'],
                'answer' => json_encode($data['answer']),
                'right' => json_encode($right),
                'update_time' => time(),
                'type' => $data['type'],
                'q_type' => $data['q_type'],
                'answer1' => $data['answer1'],
            ];
            if ($data['id']) {
                $res = QuQuestion::update($save_data, ['id' => $data['id']]);
                if ($res) {
                    QuActive::update(['total_score' => QuQuestion::getSum(['aid' => $data['aid']], 'score')], ['id' => $data['aid']]);
                    return returnJson(200, '修改成功', $data['id']);
                } else {
                    return returnJson(202, '修改失败');
                }
            } else {
                $save_data['create_time'] = time();
                $res = QuQuestion::create($save_data);
                if ($res) {
                    QuActive::update(['total_score' => QuQuestion::getSum(['aid' => $data['aid']], 'score')], ['id' => $data['aid']]);
                    return returnJson(200, '添加成功', $res['id']);
                } else {
                    return returnJson(202, '添加失败');
                }
            }

        }
    }

    /**
     * 问卷问题删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function question_del()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuQuestion::get(['id' => $data['id']]);
                if ($active) {
                    $res = QuQuestion::destroy($data['id']);
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '问卷主题不存在');
    }


    /**
     * 问卷主题
     * @return \think\response\Json
     */
    public function active()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['name']) {
                $where['name'] = [
                    'like', "%{$data['name']}%"
                ];
            }
            $list = QuActive::getPage($where, $page, $size, 'id desc');
            $total = QuActive::getCount($where);
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', [
                'list' => $list,
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }

    /**
     * 问卷主题
     * @return \think\response\Json
     */
    public function active_all()
    {
        if (request()->isPost()) {
            $list = QuActive::getAll([]);
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', $list);
        }
    }

    /**
     * 问卷主题编辑/添加
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function active_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuActive::getOne(['id' => $data['id']]);
                if (!$active) {
                    return returnJson(202, '问卷主题不存在');
                }
            }
            if (!$data['name']) {
                return returnJson(202, '请填入分类名称');
            }
            $save_data = [
                'name' => $data['name'],
                'desc' => $data['desc'],
                'update_time' => time(),
            ];
            if ($data['id']) {
                $res = QuActive::update($save_data, ['id' => $data['id']]);
                if ($res) {
                    return returnJson(200, '修改成功', $data['id']);
                } else {
                    return returnJson(202, '修改失败');
                }
            } else {
                $save_data['create_time'] = time();
                $res = QuActive::create($save_data);
                if ($res) {
                    return returnJson(200, '添加成功', $res['id']);
                } else {
                    return returnJson(202, '添加失败');
                }
            }

        }
    }

    /**
     * 问卷主题删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function active_del()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuActive::get(['id' => $data['id']]);
                if ($active) {
                    $res = QuActive::destroy($data['id']);
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '问卷主题不存在');
    }

    /**
     * 问卷班级
     * @return \think\response\Json
     */
    public function classes()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['name']) {
                $where['name'] = [
                    'like', "%{$data['name']}%"
                ];
            }
            $list = QuClasses::getPage($where, $page, $size, 'id desc');
            $total = QuClasses::getCount($where);
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', [
                'list' => $list,
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }

    /**
     * 问卷班级编辑/添加
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function classes_edit()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuClasses::getOne(['id' => $data['id']]);
                if (!$active) {
                    return returnJson(202, '问卷主题不存在');
                }
            }
            if (!$data['name']) {
                return returnJson(202, '请填入分类名称');
            }
            $save_data = [
                'name' => $data['name'],
                'update_time' => time(),
            ];
            if ($data['id']) {
                $res = QuClasses::update($save_data, ['id' => $data['id']]);
                if ($res) {
                    return returnJson(200, '修改成功', $data['id']);
                } else {
                    return returnJson(202, '修改失败');
                }
            } else {
                $save_data['create_time'] = time();
                $res = QuClasses::create($save_data);
                if ($res) {
                    return returnJson(200, '添加成功', $res['id']);
                } else {
                    return returnJson(202, '添加失败');
                }
            }

        }
    }

    /**
     * 问卷班级删除
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function classes_del()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuClasses::get(['id' => $data['id']]);
                if ($active) {
                    $res = QuClasses::destroy($data['id']);
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '问卷班级不存在');
    }

    /**
     * @return \think\response\Json
     * @throws \think\exception\DbException
     */
    public function answer_del()
    {
        if (request()->isPost()) {
            $data = input('');
            if ($data['id']) {
                $active = QuAnswer::get(['id' => $data['id']]);
                if ($active) {
                    $res = QuAnswer::destroy($data['id']);
                    if ($res) {
                        return returnJson(200, '删除成功');
                    } else {
                        return returnJson(202, '删除失败');
                    }
                }
            }
        }
        return returnJson(202, '问卷班级不存在');
    }


    /**
     * 问卷
     * @return \think\response\Json
     */
    public function classes_all()
    {
        if (request()->isPost()) {
            $list = QuClasses::getAll([]);
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', $list);
        }
    }


    /**
     * 问卷情况
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function answer()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['cid']) {
                $where['d.id'] = $data['cid'];
            }
            if ($data['aid']) {
                $where['c.id'] = $data['aid'];
            }
            if ($data['num']) {
                $where['b.num'] = $data['num'];
            }
            if ($data['name']) {
                $where['b.name'] = [
                    'like', "%{$data['name']}%"
                ];
            }

            if ($data['excel']) {
                //导出excel
                if (!$data['aid']) {
                    return returnJson(202, '请选择主题');
                }

                $list = Db::table('zkhb_qu_answer')
                    ->alias('a')
                    ->join('zkhb_qu_student2 b', 'a.sid=b.id', 'left')
                    ->join('zkhb_qu_active c', 'a.aid=c.id', 'left')
                    ->join('zkhb_qu_classes d', 'b.cid=d.id', 'left')
                    ->order('b.num asc')
                    ->where($where)
                    ->field('a.*,b.name as student,b.num,c.name as active,c.total_score,d.name as classes')
                    ->select();
                $file = $this->exportExcel($list);
                return returnJson(200, '', $file);
            }
            $quAnswer = new QuAnswer();
            $list = $quAnswer
                ->alias('a')
                ->join('zkhb_qu_student2 b', 'a.sid=b.id', 'inner')
                ->join('zkhb_qu_active c', 'a.aid=c.id', 'inner')
                ->join('zkhb_qu_classes d', 'b.cid=d.id', 'inner')
                ->page($page, $size)
                ->where($where)
                ->order('a.id desc')
                ->field('a.*,b.name as student,b.num,c.name as active,c.total_score,d.name as classes')
                ->select();
            $total = $quAnswer
                ->alias('a')
                ->join('zkhb_qu_student2 b', 'a.sid=b.id', 'inner')
                ->join('zkhb_qu_active c', 'a.aid=c.id', 'inner')
                ->join('zkhb_qu_classes d', 'b.cid=d.id', 'inner')
                ->where($where)
                ->count();
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', [
                'list' => $list->toArray(),
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }

    public function answer1()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
            if ($data['cid']) {
                $where['d.id'] = $data['cid'];
            }
            if ($data['aid']) {
                $where['c.id'] = $data['aid'];
            }
            if ($data['num']) {
                $where['b.num'] = $data['num'];
            }
            if ($data['name']) {
                $where['b.name'] = [
                    'like', "%{$data['name']}%"
                ];
            }

            if ($data['excel']) {
                //导出excel
                if (!$data['aid']) {
                    return returnJson(202, '请选择主题');
                }

                $list = Db::table('zkhb_qu_answer')
                    ->alias('a')
                    ->join('zkhb_qu_student b', 'a.sid=b.id', 'left')
                    ->join('zkhb_qu_active c', 'a.aid=c.id', 'left')
                    ->join('zkhb_qu_classes d', 'b.cid=d.id', 'left')
                    ->order('b.num asc')
                    ->where($where)
                    ->field('a.*,b.name as student,b.num,c.name as active,c.total_score,d.name as classes')
                    ->select();
                $file = $this->exportExcel($list);
                return returnJson(200, '', $file);
            }
            $quAnswer = new QuAnswer();
            $list = $quAnswer
                ->alias('a')
                ->join('zkhb_qu_student b', 'a.sid=b.id', 'inner')
                ->join('zkhb_qu_active c', 'a.aid=c.id', 'inner')
                ->join('zkhb_qu_classes d', 'b.cid=d.id', 'inner')
                ->page($page, $size)
                ->where($where)
                ->order('a.id desc')
                ->field('a.*,b.name as student,b.num,c.name as active,c.total_score,d.name as classes')
                ->select();
            $total = $quAnswer
                ->alias('a')
                ->join('zkhb_qu_student b', 'a.sid=b.id', 'inner')
                ->join('zkhb_qu_active c', 'a.aid=c.id', 'inner')
                ->join('zkhb_qu_classes d', 'b.cid=d.id', 'inner')
                ->where($where)
                ->count();
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', [
                'list' => $list->toArray(),
                'size' => $size,
                'page' => $page,
                'total' => $total
            ]);
        }
    }


    protected function exportExcel($list)
    {
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $arrData = [
            ['姓名', '班级', '学号', '问卷主题', '得分']
        ];
        $classes = QuClasses::getColumn([], 'id,name');
        foreach ($list as $k => $v) {
            if (empty($v['student'])) {
                $student = Db::table('zkhb_qu_student2')->where('id', $v['sid'])->find();
                $student1 = Db::table('zkhb_qu_student')->where('id', $v['sid'])->find();

                if ($student || $student1) {
//                    if ($student)
//                        $student = $student->toArray();
//                    if ($student1)
//                        $student1 = $student1->toArray();
                    $v['student'] = $student ? $student['name'] : $student1['name'];
                    $v['num'] = $student ? $student['num'] : $student1['num'];
                    $v['classes'] = $student ? $classes[$student['cid']] : $classes[$student1['cid']];
                }
            }


            $arrData[] = [
                $v['student'], $v['classes'], $v['num'], $v['active'], $v['score']
            ];
        }
        $sheet->fromArray($arrData, NULL);
        $writer = new Xlsx($spreadsheet);
        $name = '问卷信息-' . date('Y-m-d') . '-' . time() . ".xlsx";

        $path = ROOT_PATH . "public" . DS . "cache" . DS . $name;
        $writer->save($path);
        return request()->domain() . "/cache/" . $name;
    }

    /**
     * 导出平均成绩
     * @param $list
     * @return string
     * @throws \PhpOffice\PhpSpreadsheet\Exception
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     */
    protected function exportAvg($list)
    {

        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        $arrData = [
            ['姓名', '班级', '学号', '平均得分']
        ];
        $active = QuActive::getAll(['type' => 1]);
        $aids = array_column($active, 'id');
        if ($active) {
            foreach ($active as $value) {
                $arrData[0][] = $value['name'] . '得分';
            }
        }
        if ($list) {
            foreach ($list as $k => $v) {
                $answer = QuAnswer::getColumn(['aid' => ['in', $aids], 'sid' => $v['id']], 'aid,score');
                $total_score = QuAnswer::getSum(['aid' => ['in', $aids], 'sid' => $v['id']], 'score');
                $res = [
                    $v['name'], $v['classes'], $v['num'], round(($total_score / 12), 2)
                ];
                if ($active) {
                    foreach ($active as $value) {
                        $res[] = isset($answer[$value['id']]) ? $answer[$value['id']] : 0;
                    }
                }
                $arrData[] = $res;
            }
        }
        $sheet->fromArray($arrData, NULL);
        $writer = new Xlsx($spreadsheet);
        $name = '问卷信息-' . date('Y-m-d') . '-' . time() . ".xlsx";

        $path = ROOT_PATH . "public" . DS . "cache" . DS . $name;
        $writer->save($path);
        return request()->domain() . "/cache/" . $name;
    }


    /**
     * 问卷情况
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function answer_all()
    {
        if (request()->isPost()) {
            $data = input('');
            $where = array();
            $page = max(input('page'), 1);
            $size = max(input('size'), 20);
//            if ($data['cid']) {
//                $where['d.id'] = $data['cid'];
//            }
            if ($data['aid']) {
                $where['c.id'] = $data['aid'];
            }

            if ($data['excel']) {
                //导出excel
                $list = Db::table('zkhb_qu_answer')
                    ->alias('a')
                    ->join('zkhb_qu_student b', 'a.sid=b.id', 'left')
                    ->join('zkhb_qu_active c', 'a.aid=c.id', 'left')
                    ->join('zkhb_qu_classes d', 'b.cid=d.id', 'left')
                    ->order('b.num asc')
                    ->where($where)
                    ->field('a.*,b.name as student,b.num,c.name as active,c.total_score,d.name as classes')
                    ->select();
                $file = $this->exportExcel($list);
                return returnJson(200, '', $file);
            }
            $quAnswer = new QuAnswer();
            $list = $quAnswer
                ->alias('a')
                ->join('zkhb_qu_student b', 'a.sid=b.id', 'inner')
                ->join('zkhb_qu_active c', 'a.aid=c.id', 'inner')
                ->join('zkhb_qu_classes d', 'b.cid=d.id', 'inner')
                ->page($page, $size)
                ->where($where)
                ->field('a.*,b.name as student,c.name as active,c.total_score,d.name as classes')
                ->select();
            if (!$list) {
                return returnJson(200, '', []);
            }
            return returnJson(200, '', [
                'list' => $list->toArray(),
                'size' => $size,
                'page' => $page,
            ]);
        }
    }

    /**
     * 答题情况
     */
    public function answer_status()
    {
        $id = input('id');
        $answer = QuAnswer::getOne(['id' => $id]);
        $questions = [
            0 => [],
            1 => [],
            2 => [],
            3 => [],
            4 => []
        ];
        if ($answer && $answer['answer']) {

            $num = range('A', 'Z');
            $answer['answer'] = json_decode($answer['answer'], true);
            foreach ($answer['answer'] as $key => $value) {
                if (!in_array($key, ['aid', 'sid']) && $value) {
                    foreach ($value as $k => $v) {
                        $question = QuQuestion::getOne(['id' => $v['qid']]);
                        $question['s_answer'] = is_array($v['answer']) ? $v['answer'] : [$v['answer']];
                        if ($question['type'] != 4 && $question['type']!=5) {
                            $question['answer'] = json_decode($question['answer'], true);
                            $question['right'] = json_decode($question['right'], true);
                            foreach ($question['answer'] as $q => $a) {
                                if ($a['is_right']) {
                                    $question['right_arr'][] = $num[$q];
                                }
                                if ($question['q_type'] == 1) {
                                    $question['answer'][$q]['content1'] = $num[$q] . $a['content'];
                                } else {
                                    $question['answer'][$q]['content1'] = $num[$q];
                                }
                            }
                        }
                        $questions[$question['type'] - 1][] = $question;
                    }
                }
            }
            return returnJson(200, '', $questions);
        }
        return returnJson(200, '', $questions);

    }

    /**
     * 修改成绩
     * @return \think\response\Json
     */
    public function change_score()
    {
        if (request()->isPost()) {
            $id = input('id');
            $score = input('score');
            $answer = QuAnswer::getOne(['id' => $id]);
            if ($answer) {
                $res = QuAnswer::update(['score' => $score], ['id' => $id]);
                if ($res) {
                    return returnJson(200, '修改成功', $res);
                }
            }
        }

        return returnJson(202, '修改失败');

    }

    public function student()
    {
        $data = input('');
        $where = array();
        $page = max(input('page'), 1);
        $size = max(input('size'), 20);
        if ($data['cid']) {
            $where['a.cid'] = $data['cid'];
        }
        if ($data['num']) {
            $where['a.num'] = $data['num'];
        }
        if ($data['name']) {
            $where['a.name'] = [
                'like', "%{$data['name']}%"
            ];
        }

        $quStudent = new QuStudent();
        if ($data['excel']) {
            //导出excel
            $list = Db::table('zkhb_qu_student2')
                ->alias('a')
                ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
                ->where($where)
                ->order('a.id desc')
                ->field('a.*,d.name as classes')
                ->select();
            $file = $this->exportAvg($list->toArray());
            return returnJson(200, '', $file);
        }
        $list = Db::table('zkhb_qu_student2')
            ->alias('a')
            ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
            ->page($page, $size)
            ->where($where)
            ->order('a.id desc')
            ->field('a.*,d.name as classes')
            ->select();
        $active = QuActive::getColumn(['type' => 1], 'id,name,total_score');
        $aids = array_column($active, 'id');
        if ($list) {
            foreach ($list as $k => $v) {
                $answer = QuAnswer::getAll(['sid' => $v['id'], 'aid' => ['in', $aids]]);
                $total_score = QuAnswer::getSum(['sid' => $v['id'], 'aid' => ['in', $aids]], 'score');
                if ($answer) {
                    foreach ($answer as $key => $value) {
                        if (in_array($value['aid'], $aids)) {
                            $answer[$key]['active'] = $active[$value['aid']]['name'];
                        } else {
                            unset($answer[$key]);
                        }
                    }
                } else {
                    $answer = [];
                }
                $list[$k]['answer'] = array_merge($answer, []);
                $list[$k]['avg'] = round($total_score / sizeof($active), 2);
            }
        }
        $total = Db::table('zkhb_qu_student2')
            ->alias('a')
            ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
            ->where($where)
            ->count();
        if (!$list) {
            return returnJson(200, '', []);
        }
        return returnJson(200, '', [
            'list' => $list->toArray(),
            'size' => $size,
            'page' => $page,
            'total' => $total
        ]);
    }


    public function student1()
    {
        $data = input('');
        $where = array();
        $page = max(input('page'), 1);
        $size = max(input('size'), 20);
        if ($data['cid']) {
            $where['a.cid'] = $data['cid'];
        }
        if ($data['num']) {
            $where['a.num'] = $data['num'];
        }
        if ($data['name']) {
            $where['a.name'] = [
                'like', "%{$data['name']}%"
            ];
        }

        $quStudent = new QuStudent();
        if ($data['excel']) {
            //导出excel
            $list = Db::table('zkhb_qu_student')
                ->alias('a')
                ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
                ->where($where)
                ->order('a.id desc')
                ->field('a.*,d.name as classes')
                ->select();
            $file = $this->exportAvg($list->toArray());
            return returnJson(200, '', $file);
        }
        $list = Db::table('zkhb_qu_student')
            ->alias('a')
            ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
            ->page($page, $size)
            ->where($where)
            ->order('a.id desc')
            ->field('a.*,d.name as classes')
            ->select();
        $active = QuActive::getColumn(['type' => 1], 'id,name,total_score');
        $aids = array_column($active, 'id');
        if ($list) {
            foreach ($list as $k => $v) {
                $answer = QuAnswer::getAll(['sid' => $v['id'], 'aid' => ['in', $aids]]);
                $total_score = QuAnswer::getSum(['sid' => $v['id'], 'aid' => ['in', $aids]], 'score');
                if ($answer) {
                    foreach ($answer as $key => $value) {
                        if (isset($active[$value['aid']])) {
                            $answer[$key]['active'] = $active[$value['aid']]['name'];
                        } else {
                            unset($answer[$key]);
                        }
                    }
                } else {
                    $answer = [];
                }
                $list[$k]['answer'] = array_merge($answer, []);
                $list[$k]['avg'] = round($total_score / sizeof($active), 2);
            }
        }
        $total = $quStudent
            ->alias('a')
            ->join('zkhb_qu_classes d', 'a.cid=d.id', 'inner')
            ->where($where)
            ->count();
        if (!$list) {
            return returnJson(200, '', []);
        }
        return returnJson(200, '', [
            'list' => $list->toArray(),
            'size' => $size,
            'page' => $page,
            'total' => $total
        ]);
    }


}
